package com.archstoday.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.widget.EditText;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.common.AsRes;
import com.archstoday.common.SpfUtils;
import com.archstoday.entity.BaseEntity;
import com.archstoday.utils.OAuthUtils;
import com.archstoday.utils.ToolUtils;
import com.base.http.common.BaseHttpClient;
import com.base.utils.MySharedPreferencesMgr;
import com.base.utils.StringUtil;

/**
 * 意见反馈
 */
public class FeedbackActivity extends AsScrollBaseActivity {

    private TextView mTitleTv;
    private EditText mFeedbackEdit;

    @Override
    protected void initView(Bundle bundle) {
        setContentView(R.layout.activity_feedback);
        mTitleTv = (TextView) findViewById(R.id.title_tv);
        mFeedbackEdit = (EditText) findViewById(R.id.feedback_edit);
    }

    @Override
    protected void initData(Bundle bundle) {
        mTitleTv.setText(R.string.user_feedback);
        if (StringUtil.isEmpty(MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""))){
            showToast(R.string.user_userid_null);
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            appFinish();
        }
    }

    @Override
    protected void initListener() {
        setOnClickListener(R.id.back_btn);
        setOnClickListener(R.id.feedback_btn);
    }

    @Override
    public void treatClickEvent(int id) {
        switch (id) {
            case R.id.back_btn:
                appFinish();
                break;
            case R.id.feedback_btn:
                String str = mFeedbackEdit.getText().toString();
                if (StringUtil.isEmpty(str)) {
                    showToast(R.string.feedback_null);
                } else {
                    feedback(str);
                }
                break;
        }
    }

    private void feedback(String des) {
        initParameter();
        mParams.put("userId", MySharedPreferencesMgr.getString(SpfUtils.USER_ID, ""));
        mParams.put("token", MySharedPreferencesMgr.getString(SpfUtils.USER_TOKEN, ""));
        mParams.put("content", des);

        sendEncryptPostRequest(AsRes.getInstance().getUrl(AsRes.GET_FEEDBACK),
                AsRes.GET_FEEDBACK, mParams, BaseEntity.class);
    }

    @Override
    protected void onSuccess(String content, BaseHttpClient client, Object parse) {
        if (client.getUrlIdentifier() == AsRes.GET_FEEDBACK) {
            BaseEntity entity = (BaseEntity) parse;
            if (ToolUtils.isSuccess(entity.getStatus())) {
                appFinish();
                showToast(entity.getMsg());
            } else if (entity.getStatus() == 10005) {
                MySharedPreferencesMgr.setString(SpfUtils.USER_ID, "");
                MySharedPreferencesMgr.setString(SpfUtils.USER_TOKEN, "");
                OAuthUtils.getInstance().cleanUserInfo();

                showToast("登录信息已过期，请重新登录");
                Intent intent = new Intent(mContext, LoginActivity.class);
                startActivity(intent);
            } else {
                showToast(entity.getMsg());
            }
        }
    }
}